
using Abp.Domain.Repositories;
using Abp.UI;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.ContractManagement.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class ContractSummaryManager : DomainServiceBase, IContractSummaryManager
    {

        private readonly IRepository<ContractSummary, string> _contractsummaryRepository;

        /// <summary>
        /// 【ContractSummary】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public ContractSummaryManager(IRepository<ContractSummary, string> contractsummaryRepository)
        {
            _contractsummaryRepository = contractsummaryRepository;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用
        /// </summary>
        /// <returns></returns>
        public IQueryable<ContractSummary> QueryEntityListAsNoTracking()
        {
            var query = _contractsummaryRepository.GetAll().AsNoTracking()
                        .Select(x => new ContractSummary
                        {
                            Id = x.Id,
                            ContractRegistrationId = x.ContractRegistrationId,
                            Transactor = x.Transactor,
                            ContractStatusId = x.ContractStatusId,
                            CreationTime = x.CreationTime,
                            HouseId = x.HouseId,
                            AgentIdNumber = x.AgentIdNumber,
                            AgentName = x.AgentName,
                            ContractTypeCode = x.ContractTypeCode,
                            CertificateName = x.CertificateName,
                            CertificateCode = x.CertificateCode,
                            BrokerageInstitutionId = x.BrokerageInstitutionId,
                            BrokerageInstitutionName = x.BrokerageInstitutionName,
                            BuildingArea = x.BuildingArea,
                            PartyAPassword = x.PartyAPassword,
                            PartyBPassword = x.PartyBPassword,
                            AttentionLine = x.AttentionLine,
                            HouseUsage = x.HouseUsage,
                            HouseProperty = x.HouseProperty,
                            SystemPlatform = x.SystemPlatform,
                            CertificateName_2 = x.CertificateName_2,
                            EntrustmentContractId = x.EntrustmentContractId,
                            ContractHandlingStatusCode = x.ContractHandlingStatusCode,
                            LocatedInformation = x.LocatedInformation,
                            PaymentMethodCode = x.PaymentMethodCode,
                            ContractValidityDays = x.ContractValidityDays,
                            HTYXQ = x.HTYXQ,
                            ContractModelConfigCode = x.ContractModelConfigCode,
                            ContractIdentification = x.ContractIdentification,
                            Seller = x.Seller,
                            Buyer = x.Buyer,
                            SellerIdCardNumber = x.SellerIdCardNumber,
                            BuyerIdCardNumber = x.BuyerIdCardNumber,
                            IsMortgage = x.IsMortgage,
                            SBBZ = x.SBBZ,
                            IsNeedRecheck = x.IsNeedRecheck,
                            RecheckOrganizationId = x.RecheckOrganizationId,
                            RecheckOrganizationName = x.RecheckOrganizationName,
                            RecheckPeopleId = x.RecheckPeopleId,
                            RecheckPeopleName = x.RecheckPeopleName,
                            ContractOwnerOrganizationId = x.ContractOwnerOrganizationId,
                            ImmovablesWarrantIdCode = x.ImmovablesWarrantIdCode,
                            ContractStatus = x.ContractStatus,
                        });

            return query;
        }

        /// <summary>
        /// 【ContractSummary】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<ContractSummary> Query()
        {
            return _contractsummaryRepository.GetAll();
        }

        /// <summary>
        /// 【ContractSummary】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<ContractSummary> QueryAsNoTracking()
        {
            return _contractsummaryRepository.GetAll().AsNoTracking();
        }

        /// <summary>
        /// 【ContractSummary】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<ContractSummary> FindByIdAsync(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new UserFriendlyException("Id不能为空！");
            }
            var entity = await _contractsummaryRepository.GetAsync(id);
            return entity;
        }

        /// <summary>
        /// 【ContractSummary】检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool</returns>
        public async Task<bool> IsExistAsync(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new UserFriendlyException("Id不能为空！");
            }
            var result = await _contractsummaryRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }

        /// <summary>
        /// 【ContractSummary】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ContractSummary> CreateAsync(ContractSummary entity)
        {
            entity.Id = await _contractsummaryRepository.InsertAndGetIdAsync(entity);
            return entity;
        }

        /// <summary>
        /// 【ContractSummary】更新实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateAsync(ContractSummary entity)
        {
            await _contractsummaryRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 【ContractSummary】删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new UserFriendlyException("Id不能为空！");
            }
            //TODO:删除前的逻辑判断，是否允许删除
            await _contractsummaryRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 【ContractSummary】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDeleteAsync(ICollection<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _contractsummaryRepository.DeleteAsync(a => input.Contains(a.Id));
        }
        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展领域服务接口*/

        #endregion

    }
}
